import Vue from 'vue'
import Vuex from 'vuex'

import {
	getUserInfo
} from '@/common/api.js'

import {
	baseUrl
} from '@/common/config.js'

Vue.use(Vuex) // vue的插件机制

// Vuex.Store 构造器选项
const store = new Vuex.Store({
	// 为了不和页面或组件的data中的造成混淆，state中的变量前面建议加上$符号
	state: {
		systemInfo: null,
		// 用户信息
		$userInfo: uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo') : {},
		token: uni.getStorageSync('token') ? uni.getStorageSync('token') : null,
		isLogin: uni.getStorageSync('token') ? true : false,
	},
	mutations: {
		setSystemInfo(state, payload) {
			state.systemInfo = payload;
		},
		setUserInfo: (state, data) => {
			uni.setStorageSync('userInfo', data);
			uni.setStorageSync('token', data.token);

			state.$userInfo = uni.getStorageSync('userInfo');
			state.token = uni.getStorageSync('token');
			state.isLogin = true;
		}
	},
	actions: {
		getUserInfo(context) {
			getUserInfo().then(res => {
				context.commit('setUserInfo', res.data);
			}).catch(err => {
				return Promise.reject(err)
			})
		}
	}
})

export default store